home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / python2.6 / shutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2013-01-10  |  9.3 KB  |  364 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Utility functions for copying files and directory trees.
  5.  
  6. XXX The functions here don't copy the resource fork or other metadata on Mac.
  7.  
  8. """
  9. import os
  10. import sys
  11. import stat
  12. from os.path import abspath
  13. import fnmatch
  14. import errno
  15. __all__ = [
  16.     'copyfileobj',
  17.     'copyfile',
  18.     'copymode',
  19.     'copystat',
  20.     'copy',
  21.     'copy2',
  22.     'copytree',
  23.     'move',
  24.     'rmtree',
  25.     'Error']
  26.  
  27. class Error(EnvironmentError):
  28.     pass
  29.  
  30.  
  31. try:
  32.     WindowsError
  33. except NameError:
  34.     WindowsError = None
  35.  
  36.  
  37. def copyfileobj(fsrc, fdst, length = 16384):
  38.     '''copy data from file-like object fsrc to file-like object fdst'''
  39.     while None:
  40.         buf = fsrc.read(length)
  41.         if not buf:
  42.             break
  43.         
  44.         continue
  45.         return None
  46.  
  47.  
  48. def _samefile(src, dst):
  49.     if hasattr(os.path, 'samefile'):
  50.         
  51.         try:
  52.             return os.path.samefile(src, dst)
  53.         except OSError:
  54.             return False
  55.         
  56.  
  57.     None<EXCEPTION MATCH>OSError
  58.     return os.path.normcase(os.path.abspath(src)) == os.path.normcase(os.path.abspath(dst))
  59.  
  60.  
  61. def copyfile(src, dst):
  62.     '''Copy data from src to dst'''
  63.     if _samefile(src, dst):
  64.         raise Error('`%s` and `%s` are the same file' % (src, dst))
  65.     _samefile(src, dst)
  66.     
  67.     try:
  68.         fsrc = _[1]
  69.         
  70.         try:
  71.             fdst = _[2]
  72.             copyfileobj(fsrc, fdst)
  73.         finally:
  74.             pass
  75.  
  76.     finally:
  77.         pass
  78.  
  79.  
  80.  
  81. def copymode(src, dst):
  82.     '''Copy mode bits from src to dst'''
  83.     if hasattr(os, 'chmod'):
  84.         st = os.stat(src)
  85.         mode = stat.S_IMODE(st.st_mode)
  86.         os.chmod(dst, mode)
  87.     
  88.  
  89.  
  90. def copystat(src, dst):
  91.     '''Copy all stat info (mode bits, atime, mtime, flags) from src to dst'''
  92.     st = os.stat(src)
  93.     mode = stat.S_IMODE(st.st_mode)
  94.     if hasattr(os, 'utime'):
  95.         os.utime(dst, (st.st_atime, st.st_mtime))
  96.     
  97.     if hasattr(os, 'chmod'):
  98.         os.chmod(dst, mode)
  99.     
  100.     if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
  101.         
  102.         try:
  103.             os.chflags(dst, st.st_flags)
  104.         except OSError:
  105.             why = None
  106.             if not hasattr(errno, 'EOPNOTSUPP') or why.errno != errno.EOPNOTSUPP:
  107.                 raise 
  108.             why.errno != errno.EOPNOTSUPP
  109.         except:
  110.             None<EXCEPTION MATCH>OSError
  111.         
  112.  
  113.     None<EXCEPTION MATCH>OSError
  114.  
  115.  
  116. def copy(src, dst):
  117.     '''Copy data and mode bits ("cp src dst").
  118.  
  119.     The destination may be a directory.
  120.  
  121.     '''
  122.     if os.path.isdir(dst):
  123.         dst = os.path.join(dst, os.path.basename(src))
  124.     
  125.     copyfile(src, dst)
  126.     copymode(src, dst)
  127.  
  128.  
  129. def copy2(src, dst):
  130.     '''Copy data and all stat info ("cp -p src dst").
  131.  
  132.     The destination may be a directory.
  133.  
  134.     '''
  135.     if os.path.isdir(dst):
  136.         dst = os.path.join(dst, os.path.basename(src))
  137.     
  138.     copyfile(src, dst)
  139.     copystat(src, dst)
  140.  
  141.  
  142. def ignore_patterns(*patterns):
  143.     '''Function that can be used as copytree() ignore parameter.
  144.  
  145.     Patterns is a sequence of glob-style patterns
  146.     that are used to exclude files'''
  147.     
  148.     def _ignore_patterns(path, names):
  149.         ignored_names = []
  150.         for pattern in patterns:
  151.             ignored_names.extend(fnmatch.filter(names, pattern))
  152.         
  153.         return set(ignored_names)
  154.  
  155.     return _ignore_patterns
  156.  
  157.  
  158. def copytree(src, dst, symlinks = False, ignore = None):
  159.     '''Recursively copy a directory tree using copy2().
  160.  
  161.     The destination directory must not already exist.
  162.     If exception(s) occur, an Error is raised with a list of reasons.
  163.  
  164.     If the optional symlinks flag is true, symbolic links in the
  165.     source tree result in symbolic links in the destination tree; if
  166.     it is false, the contents of the files pointed to by symbolic
  167.     links are copied.
  168.  
  169.     The optional ignore argument is a callable. If given, it
  170.     is called with the `src` parameter, which is the directory
  171.     being visited by copytree(), and `names` which is the list of
  172.     `src` contents, as returned by os.listdir():
  173.  
  174.         callable(src, names) -> ignored_names
  175.  
  176.     Since copytree() is called recursively, the callable will be
  177.     called once for each directory that is copied. It returns a
  178.     list of names relative to the `src` directory that should
  179.     not be copied.
  180.  
  181.     XXX Consider this example code rather than the ultimate tool.
  182.  
  183.     '''
  184.     names = os.listdir(src)
  185.     if ignore is not None:
  186.         ignored_names = ignore(src, names)
  187.     else:
  188.         ignored_names = set()
  189.     os.makedirs(dst)
  190.     errors = []
  191.     for name in names:
  192.         if name in ignored_names:
  193.             continue
  194.         
  195.         srcname = os.path.join(src, name)
  196.         dstname = os.path.join(dst, name)
  197.         
  198.         try:
  199.             if symlinks and os.path.islink(srcname):
  200.                 linkto = os.readlink(srcname)
  201.                 os.symlink(linkto, dstname)
  202.             elif os.path.isdir(srcname):
  203.                 copytree(srcname, dstname, symlinks, ignore)
  204.             else:
  205.                 copy2(srcname, dstname)
  206.         continue
  207.         except (IOError, os.error):
  208.             why = None
  209.             errors.append((srcname, dstname, str(why)))
  210.             continue
  211.             except Error:
  212.                 err = None
  213.                 errors.extend(err.args[0])
  214.                 continue
  215.             
  216.         try:
  217.             copystat(src, dst)
  218.         except OSError:
  219.             None<EXCEPTION MATCH>(IOError, os.error)
  220.             why = None<EXCEPTION MATCH>(IOError, os.error)
  221.             if WindowsError is not None and isinstance(why, WindowsError):
  222.                 pass
  223.             else:
  224.                 errors.extend((src, dst, str(why)))
  225.         except:
  226.             isinstance(why, WindowsError)
  227.  
  228.         if errors:
  229.             raise Error, errors
  230.         errors
  231.         return None
  232.  
  233.  
  234. def rmtree(path, ignore_errors = False, onerror = None):
  235.     '''Recursively delete a directory tree.
  236.  
  237.     If ignore_errors is set, errors are ignored; otherwise, if onerror
  238.     is set, it is called to handle the error with arguments (func,
  239.     path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
  240.     path is the argument to that function that caused it to fail; and
  241.     exc_info is a tuple returned by sys.exc_info().  If ignore_errors
  242.     is false and onerror is None, an exception is raised.
  243.  
  244.     '''
  245.     if ignore_errors:
  246.         
  247.         def onerror(*args):
  248.             pass
  249.  
  250.     elif onerror is None:
  251.         
  252.         def onerror(*args):
  253.             raise 
  254.  
  255.     
  256.     
  257.     try:
  258.         if os.path.islink(path):
  259.             raise OSError('Cannot call rmtree on a symbolic link')
  260.         os.path.islink(path)
  261.     except OSError:
  262.         onerror(os.path.islink, path, sys.exc_info())
  263.         return None
  264.  
  265.     names = []
  266.     
  267.     try:
  268.         names = os.listdir(path)
  269.     except os.error:
  270.         err = None
  271.         onerror(os.listdir, path, sys.exc_info())
  272.  
  273.     for name in names:
  274.         fullname = os.path.join(path, name)
  275.         
  276.         try:
  277.             mode = os.lstat(fullname).st_mode
  278.         except os.error:
  279.             mode = 0
  280.  
  281.         if stat.S_ISDIR(mode):
  282.             rmtree(fullname, ignore_errors, onerror)
  283.             continue
  284.         
  285.         try:
  286.             os.remove(fullname)
  287.         continue
  288.         except os.error:
  289.             err = None
  290.             onerror(os.remove, fullname, sys.exc_info())
  291.             continue
  292.         
  293.  
  294.     
  295.     
  296.     try:
  297.         os.rmdir(path)
  298.     except os.error:
  299.         None<EXCEPTION MATCH>os.error
  300.         None<EXCEPTION MATCH>os.error
  301.         onerror(os.rmdir, path, sys.exc_info())
  302.     except:
  303.         None<EXCEPTION MATCH>os.error
  304.  
  305.  
  306.  
  307. def _basename(path):
  308.     return os.path.basename(path.rstrip(os.path.sep))
  309.  
  310.  
  311. def move(src, dst):
  312.     '''Recursively move a file or directory to another location. This is
  313.     similar to the Unix "mv" command.
  314.  
  315.     If the destination is a directory or a symlink to a directory, the source
  316.     is moved inside the directory. The destination path must not already
  317.     exist.
  318.  
  319.     If the destination already exists but is not a directory, it may be
  320.     overwritten depending on os.rename() semantics.
  321.  
  322.     If the destination is on our current filesystem, then rename() is used.
  323.     Otherwise, src is copied to the destination and then removed.
  324.     A lot more could be done here...  A look at a mv.c shows a lot of
  325.     the issues this implementation glosses over.
  326.  
  327.     '''
  328.     real_dst = dst
  329.     if os.path.isdir(dst):
  330.         real_dst = os.path.join(dst, _basename(src))
  331.         if os.path.exists(real_dst):
  332.             raise Error, "Destination path '%s' already exists" % real_dst
  333.         os.path.exists(real_dst)
  334.     
  335.     
  336.     try:
  337.         os.rename(src, real_dst)
  338.     except OSError:
  339.         if os.path.isdir(src):
  340.             if destinsrc(src, dst):
  341.                 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
  342.             destinsrc(src, dst)
  343.             copytree(src, real_dst, symlinks = True)
  344.             rmtree(src)
  345.         else:
  346.             copy2(src, real_dst)
  347.             os.unlink(src)
  348.     except:
  349.         os.path.isdir(src)
  350.  
  351.  
  352.  
  353. def destinsrc(src, dst):
  354.     src = abspath(src)
  355.     dst = abspath(dst)
  356.     if not src.endswith(os.path.sep):
  357.         src += os.path.sep
  358.     
  359.     if not dst.endswith(os.path.sep):
  360.         dst += os.path.sep
  361.     
  362.     return dst.startswith(src)
  363.  
  364.